package cn.me.controller;

import cn.me.pojo.Pay;
import cn.me.pojo.Payrank;
import cn.me.pojo.User;
import cn.me.pojo.toolPojo.ToCount;
import cn.me.service.PayRankService;
import cn.me.service.PayService;
import cn.me.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private PayService payService;
    @Autowired
    private UserService userService;
    @Autowired
    private PayRankService payRankService;

    @RequestMapping(value = "/mypay")
    public String mypay(int puid,
                        @RequestParam(value = "dateAfter",defaultValue = "")String dateAfter,
                        @RequestParam(value = "dateBefore",defaultValue = "")String dateBefore,
                        @RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "size",defaultValue = "3")Integer size,
                        Model model
                        ){

        PageInfo<Pay> pageInfo = payService.selectby(puid, dateAfter, dateBefore, page, size);
        model.addAttribute("pageInfo2",pageInfo);
        model.addAttribute("dateAfter",dateAfter);
        model.addAttribute("dateBefore",dateBefore);
        model.addAttribute("puid",puid);
        System.out.println(dateAfter);
        return "mypay";
    }

    //去添加工资页面
    @RequestMapping("/add/{id}")
    public String add(@PathVariable int id,Model model){
        User user = userService.selectByUid(id);
        Payrank payrank = payRankService.selectByRid(user.getUrank());


        model.addAttribute("user",user);
        model.addAttribute("payrank",payrank);

        return "mypayAdd";

    }

    //添加工资
    @RequestMapping("/toAdd")
    public String toAdd(Pay pay, HttpServletResponse response){
        System.out.println(pay);
        payService.insert(pay);

        return "redirect:/pay/mypay?puid="+pay.getPuid();

    }

    //ajax计算五险一金，个税，实发
    @ResponseBody
    @RequestMapping(value = "/toCount",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String toCount(ToCount count){
        System.out.println(count.getPsubsidy()+""+count.getPment());
        Pay pay = payService.toCount(count);
        String s = JSON.toJSONString(pay, true);
        System.out.println(s);

        return s;


    }

}
